{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 修改列表\n",
    "\n",
    "- 只适用于可变序列，不可变序列无法通过索引来修改"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "## 直接通过索引修改元素"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(my_city)\n",
    "\n",
    "my_city[0] = 'Beijing'\n",
    "print(my_city)"
   ]
  },
  {
   "source": [
    "## 通过 `del` 来删除元素"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(my_city)\n",
    "\n",
    "del my_city[2]\n",
    "print(my_city)"
   ]
  },
  {
   "source": [
    "## 通过切片修改列表\n",
    "\n",
    "- 在给切片进行赋值时，只能使用序列\n",
    "- 当设置了除 `1` 以外的步长时，序列中元素的个数必须和切片中元素的个数一致"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(my_city)\n",
    "\n",
    "my_city[0:3] = ['Shang Hai','Hang Zhou','Hong Kong']\n",
    "print(my_city)\n",
    "\n",
    "my_city[0:3] = '012'\n",
    "print(my_city)\n",
    "\n",
    "my_city[0:0] = ['Xi An'] # 向索引为 0 的位置插入元素\n",
    "print(my_city)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(my_city)\n",
    "\n",
    "my_city[::2] = ['Beijing','Xi An','Shang Hai']\n",
    "print(my_city)"
   ]
  },
  {
   "source": [
    "## 通过切片删除元素"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(my_city)\n",
    "\n",
    "del my_city[:2]\n",
    "print(my_city)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(my_city)\n",
    "\n",
    "del my_city[::2]\n",
    "print(my_city)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "print(my_city)\n",
    "\n",
    "my_city[:2] = []\n",
    "print(my_city)"
   ]
  },
  {
   "source": [
    "## `list()`\n",
    "\n",
    "- 可以通过 `list()` 函数将其它的序列转换为 `list`"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_name = 'icekylin'\n",
    "print(my_name)\n",
    "\n",
    "my_name = list(my_name)\n",
    "print(my_name)"
   ]
  }
 ]
}